package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.SensitiveDto;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-01-14
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {
    @Override
    public ResponseResult listSensitive(SensitiveDto sensitiveDto) {

        IPage<WmSensitive> page = new Page<>(sensitiveDto.getPage(), sensitiveDto.getSize());

        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(sensitiveDto.getName())) {

            queryWrapper.like(WmSensitive::getSensitives, sensitiveDto.getName());
        }

        queryWrapper.orderByDesc(WmSensitive::getCreatedTime);

        IPage<WmSensitive> page1 = this.page(page, queryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage(), sensitiveDto.getSize(), page1.getTotal(), page1.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult saveSensitive(WmSensitive entity) {

        if (entity == null || StringUtils.isEmpty(entity.getSensitives())) {

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(WmSensitive::getSensitives, entity.getSensitives());

        WmSensitive one = this.getOne(queryWrapper);

        if (one != null) {

            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        entity.setCreatedTime(new Date());

        this.save(entity);

        return ResponseResult.okResult(entity.getId());
    }
}
